package com.team.cluedo.fc;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;

import com.team.cluedo.app.ChoosePerso;
import com.team.cluedo.app.MakeHypothese;
import com.team.cluedo.app.PlayerGame;
import com.team.cluedo.app.ReceiveOneCard;
import com.team.cluedo.app.SendOneCard;
import com.team.cluedo.app.waitscreens.WaitForYourTurn;
import com.team.communication.CallClientMethods;


public class Cluedo implements Serializable {
	

	private static final long serialVersionUID = -4475543613629152428L;
	
	String[] existGames = null;
	boolean wait = true;
	boolean waitForTurn = true;
	boolean createdGame = true;
	boolean joinGame = true;
	boolean chooseCharacter = true;
	boolean makeSuggestion = false;
	boolean hasAskedCard = false;
	boolean showCard = false;
	
	//la carte recu
	public String askedCard = "noAskedCard";
	
	//les cartes recues dont faut il faut en renvoyer une
	public String suggestedCards[] = new String[0];
	
	int characterPosition = 0;
	int weaponPosition = 0;
	
	boolean secretPassage = false;
	PossibleDirections direction = new PossibleDirections(true, true, true, true);
	
	//salle dans laquelle se trouve le personnage
	public String position = "kitchen";
	int room;
	
	String name;
	public Activity activity;
	
	public int nbOfMove = 0;
	private String lastDeplacement = "";
	
	
	public String[] nameOfCharacter = new String[]{"green","mustard","scarlet","peacock","white","plum"};
	public String[] nameOfRoom = new String[]{"hall","ballroom","conservatory","billard_room","library","study","lounge","dining_room","kitchen"};
	public String[] nameOfWeapon = new String[]{"candlestick","dagger","lead_pipe","revolver","rope","spanner"};
	
	
	public Card[] ficheOfCharacter = new Card[]{new Card("green"),new Card("mustard"),new Card("scarlet"),new Card("peacock"),new Card("white"),new Card("plum")};
	public Card[] ficheOfRoom = new Card[]{new Card("hall"),new Card("ballroom"),new Card("conservatory"),new Card("billard_room"),new Card("library"),new Card("study"),new Card("lounge"),new Card("dining_room"),new Card("kitchen")};
	public Card[] ficheOfWeapon = new Card[]{new Card("candlestick"),new Card("dagger"),new Card("lead_pipe"),new Card("revolver"),new Card("rope"),new Card("spanner")};
	
	
	public String[] mesCartesPerso = new String[]{"green","scarlet","white"};
	public String[] mesCartesPiece = new String[]{"hall","library"};
	public String[] mesCartesArme = new String[]{"dagger","lead_pipe","lead_pipe","spanner"};
	
	
	String notes;
	
	public boolean hasAskedCard(){
		return hasAskedCard;
	}

	public String getNameOfCharacter(int position){
		return nameOfCharacter[position];
	}
	public String getNameOfWeapon(int position){
		return nameOfWeapon[position];
	}
	
	public Cluedo() {
		
	}
	public void setWait(){
		wait = false;
	}
	public void joinGame(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public String[] getExistGames() {
		return existGames;
	}

	public void setCreatedGameFalse(){
		createdGame = false;
	}
	public void setExistGames(String[] existGames) {
		this.existGames = existGames;
	}
	public void setJoinGamesFalse() {
		joinGame = false;
	}
	public int getRoom(){
		for(int i=0;i<nameOfRoom.length;i++){
			if(nameOfRoom[i].equalsIgnoreCase(position)){
				return i;
			}
		}
		return 0;
	}
	public boolean waitForCreateGame(String name,int nbPlayer) {
		CallClientMethods.createGame(name,nbPlayer);
		
		waitMessage();
		
		return createdGame;
		
	}
	public String[] waitForNames() {
		CallClientMethods.askExistGames();

		while(existGames == null) {}

		return existGames;
	}

	public boolean waitJoinGame(String name) {
		CallClientMethods.joinGame(name);
		
		//waitMessage();
		
		return joinGame;
	}
	
	
	public boolean chooseCharacter(int index) { 
		CallClientMethods.choosePawn(nameOfCharacter[index]);
		
		//waitMessage();
		if(chooseCharacter){
			name = nameOfCharacter[index];
		}
		
		return chooseCharacter;
		
	}
	
	
	public void waitForDiceScore() {
		
		// aleatoir number TODO
		CallClientMethods.sendScoreDices(3, 4);
		nbOfMove = 8;
		
		waitMessage();
		
	}
	
	
	private void waitMessage(){
		wait = true;
		while(wait) {}
		wait = true;
	}
	
	public void setDirection(String[] param) {
		direction.setNorth(Boolean.parseBoolean(param[0]));
		direction.setSouth(Boolean.parseBoolean(param[1]));
		direction.setEast(Boolean.parseBoolean(param[2]));
		direction.setWest(Boolean.parseBoolean(param[3]));
		secretPassage = Boolean.parseBoolean(param[4]);
	}
	public void waitMove(String string) {
		CallClientMethods.sendDeplacement(string);
		nbMove(string);
		waitMessage();
	}
	
	private void nbMove(String dep){
		if (lastDeplacement.equalsIgnoreCase("north")) {
			if (dep.equalsIgnoreCase("south")) {
				nbOfMove--;
			} else {
				nbOfMove++;
			}
		}
		else if (lastDeplacement.equalsIgnoreCase("south")) {
			if (dep.equalsIgnoreCase("north")) {
				nbOfMove--;
			} else {
				nbOfMove++;
			}
		}
		else if (lastDeplacement.equalsIgnoreCase("east")) {
			if (dep.equalsIgnoreCase("west")) {
				nbOfMove--;
			} else {
				nbOfMove++;
			}
		}
		else if (lastDeplacement.equalsIgnoreCase("west")) {
			if (dep.equalsIgnoreCase("east")) {
				nbOfMove--;
			} else {
				nbOfMove++;
			}
		}
		else if (lastDeplacement.equalsIgnoreCase("")) {
			nbOfMove++;
		}
		lastDeplacement = dep;
	}
	
	
	public boolean getDirections(String dir){
		if(dir.equalsIgnoreCase("north")){
			return direction.isNorth();
		}
		else if(dir.equalsIgnoreCase("south")){
			return direction.isSouth();
		}
		else if(dir.equalsIgnoreCase("east")){
			return direction.isEast();
		}
		else if(dir.equalsIgnoreCase("west")){
			return direction.isWest();
		}
		return true;
	}
	public void waitForValidedMove() {
		CallClientMethods.valideMove();
		lastDeplacement = "";
		//waitMessage();
		
	}
	public void setPosition(String string) {
		position = string;
		
	}

	public void waitForPlay() {
		
		waitMessage();
	}

	public void setMakeSuggestionTrue(String room) {
		this.room = getRoom(room);
		this.position = room;
		Intent defineIntent = new Intent(activity,MakeHypothese.class);
		activity.startActivity(defineIntent);
	}
	

	private int getRoom(String room) {
		for(int i=0;i<nameOfRoom.length;i++){
			if(nameOfRoom[i].equalsIgnoreCase(room)){
				return i;
			}
		}
		return 0;
	}
	
	public boolean getMakeSuggestion(){
		return makeSuggestion;
	}

	public void waitForSuggestion(int posChar, int posRoom, int posWeapon) {
		CallClientMethods.sendSuggestion(nameOfCharacter[posChar], nameOfRoom[posRoom], nameOfWeapon[posWeapon]);
		waitMessage();
	}

	public void waitForSuggestion(int posChar, int posWeapon) {
		CallClientMethods.sendSuggestion(nameOfCharacter[posChar], nameOfRoom[room], nameOfWeapon[posWeapon]);
		//waitMessage();
	}
	public void setHasAskedCardTrue() {
		hasAskedCard = true;
		
	}

	public void setAskedCard(String string) {
		askedCard = string;
		Intent defineIntent = new Intent(activity,ReceiveOneCard.class);
		activity.startActivity(defineIntent);
	}
	
	public void setAskedCard() {
		askedCard = "noAskedCard";
		Intent defineIntent = new Intent(activity,ReceiveOneCard.class);
		activity.startActivity(defineIntent);
		
	}	
	

	public void setSuggestedCards(String[] param) {
		suggestedCards = param;
		showCard = true;
		Intent defineIntent = new Intent(activity,SendOneCard.class);
		activity.startActivity(defineIntent);
	}
	
	public String getSuggestedCards(int param) {
		return suggestedCards[param];
	}

	public void waitForFinishedTurn() {
		CallClientMethods.sendReturnCard();
		//waitMessageTurn(); //wait for turn 
		Intent defineIntent = new Intent(activity,WaitForYourTurn.class);
		activity.startActivity(defineIntent);
	}

	private void waitMessageTurn() {
		waitForTurn = true;
		while(waitForTurn){
			if(showCard){
				System.out.println("show card :");
				for(int i=0;i<suggestedCards.length;i++){
					System.out.println(suggestedCards[i]);
				}
			}
		}
		
		existGames = null;
		wait = true;
		waitForTurn = true;
		createdGame = true;
		joinGame = true;
		chooseCharacter = true;
		makeSuggestion = false;
		hasAskedCard = false;
		secretPassage = false;
		showCard = false;
		direction = new PossibleDirections(true, true, true, true);
		
		
	}

	public void setWaitTurn() {
		waitForTurn = false;
	}

	public void setActivity(Activity activity) {
		//this.activity = activity;
	}
	
	
	public void startTurn(){
		Intent defineIntent = new Intent(activity,PlayerGame.class);
		activity.startActivity(defineIntent);
	}

	

	public void setChooseCharacterFalse() {
		Intent defineIntent = new Intent(activity,ChoosePerso.class);
		activity.startActivity(defineIntent);
	}
	
	
	public void showCards() {
		//passe en parametre les cartes a affich 
		//this.suggestedCards
		//si pas de carte passer un parametre qui dit qu'on en a pas !:
		
		Intent defineIntent = new Intent(activity,SendOneCard.class);
		activity.startActivity(defineIntent);
		
	}
	
	public void suggestedCards() {
		//passe en parametre les cartes a affich 
		//this.suggestedCards
		//si pas de carte passer un parametre qui dit qu'on en a pas !:
		
		Intent defineIntent = new Intent(activity,ReceiveOneCard.class);
		activity.startActivity(defineIntent);
		
	}

	public void returnAskedCard(int element) {
		CallClientMethods.returnAskedCard(suggestedCards[element]);
		Intent defineIntent = new Intent(activity,WaitForYourTurn.class);
		activity.startActivity(defineIntent);
		
		
	}
	public void returnAskedCard() {
		CallClientMethods.returnAskedCard();
		Intent defineIntent = new Intent(activity,WaitForYourTurn.class);
		activity.startActivity(defineIntent);
	}

	public void choosePerso() {
		Intent defineIntent = new Intent(activity,ChoosePerso.class);
		activity.startActivity(defineIntent);
		
	}
	public String nomJoueurCourant = "";
	public void waitMyTurn(String name) {
		nomJoueurCourant = name;
		Intent defineIntent = new Intent(activity,WaitForYourTurn.class);
		activity.startActivity(defineIntent);
	}
	
	public void waitMyTurn() {
		Intent defineIntent = new Intent(activity,WaitForYourTurn.class);
		activity.startActivity(defineIntent);
	}

	public void setMyCards(String[] cartes) {
		ArrayList<String> perso = new ArrayList<String>();
		ArrayList<String> arme = new ArrayList<String>();
		ArrayList<String> piece = new ArrayList<String>();
		
		
    	for(int i=0;i<cartes.length;i++){
    		
    		//Personnage
    		if(cartes[i].equalsIgnoreCase("green")){
    			perso.add("green");
    		}
    		if(cartes[i].equalsIgnoreCase("mustard")){
    			perso.add("mustard");    		
    		}
    		if(cartes[i].equalsIgnoreCase("scarlet")){
    			perso.add("scarlet");    		
    		}
    		if(cartes[i].equalsIgnoreCase("peacock")){
    			perso.add("peacock");    		
    		}
    		if(cartes[i].equalsIgnoreCase("white")){
    			perso.add("white");    		
    		}
    		if(cartes[i].equalsIgnoreCase("plum")){
    			perso.add("plum");    		
    		}
    		
    		
    		//Salle"hall","ballroom","conservatory","billard_room","library","study","lounge","dining_room","kitchen"
    		
			if (cartes[i].equalsIgnoreCase("hall")) {
				piece.add("hall");
			}
			if (cartes[i].equalsIgnoreCase("ballroom")) {
				piece.add("ballroom");
			}
			if (cartes[i].equalsIgnoreCase("conservatory")) {
				piece.add("conservatory");
			}
			if (cartes[i].equalsIgnoreCase("billard_room")) {
				piece.add("billard_room");
			}
			if (cartes[i].equalsIgnoreCase("study")) {
				piece.add("study");
			}
			if (cartes[i].equalsIgnoreCase("lounge")) {
				piece.add("lounge");
			}
			if (cartes[i].equalsIgnoreCase("dining_room")) {
				piece.add("dining_room");
			}
			if (cartes[i].equalsIgnoreCase("kitchen")) {
				piece.add("kitchen");
			}
    		
    		//arme "candlestick","dagger","lead_pipe","revolver","rope","spanner"
    		
			if (cartes[i].equalsIgnoreCase("candlestick")) {
				arme.add("candlestick");
			}
			if (cartes[i].equalsIgnoreCase("dagger")) {
				arme.add("dagger");
			}
			if (cartes[i].equalsIgnoreCase("lead_pipe")) {
				arme.add("lead_pipe");
			}
			if (cartes[i].equalsIgnoreCase("revolver")) {
				arme.add("revolver");
			}
			if (cartes[i].equalsIgnoreCase("rope")) {
				arme.add("rope");
			}
			if (cartes[i].equalsIgnoreCase("spanner")) {
				arme.add("spanner");
			}
    	
    		mesCartesArme = new String[arme.size()];
    		mesCartesPerso = new String[perso.size()];
    		mesCartesPiece = new String[piece.size()];
    		for(i=0;i<arme.size();i++){
    			mesCartesArme[i] = arme.get(i);
    		}
			for (i = 0; i < perso.size(); i++) {
				mesCartesPerso[i] = perso.get(i);
			}
			for (i = 0; i < piece.size(); i++) {
				mesCartesPiece[i] = piece.get(i);
			}
    	
    	}
		
	}
}
